/*
 * Copyright 2011 ArcBees Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.arcbees.test.collaborative.server.diffsync;

import com.arcbees.test.collaborative.server.cache.DiffSyncCache;
import com.arcbees.test.collaborative.server.cache.DocumentShadowCache;
import com.arcbees.test.collaborative.server.cache.ServerCache;
import com.arcbees.test.collaborative.server.cache.ServerCacheFactory;
import com.arcbees.test.collaborative.shared.diffsync.AbstractDocumentShadow;
import com.arcbees.test.collaborative.shared.diffsync.DiffHandler;
import com.arcbees.test.collaborative.shared.diffsync.DocumentShadow;
import com.google.inject.Inject;

public final class ServerDocumentShadowImpl extends AbstractDocumentShadow {

  private static final DocumentType docType = DocumentType.Server;
  private final ServerCacheFactory serverCacheFactory;
  private ServerCache<DocumentShadowCache> backupCache;
  private final DiffHandler diffHandler;

  @Inject
  public ServerDocumentShadowImpl(DiffHandler diffHandler,
      ServerCacheFactory serverCacheFactory) {
    super(diffHandler);
    this.diffHandler = diffHandler;
    this.serverCacheFactory = serverCacheFactory;
  }

  @Override
  protected DocumentType getDocType() {
    return docType;
  }

  @Override
  public int getTargetVersion() {
    return getClientVersion();
  }

  @Override
  public int getVersion() {
    return getServerVersion();
  }

  @Override
  protected void loadCache() {
    if (backupCache == null)
      backupCache = serverCacheFactory.create(DiffSyncCache.getBackupDocShadowKey(getId()));

    DocumentShadowCache docCache = backupCache.getObject();
    if (docCache != null) {
      if (backupShadow == null) {
        backupShadow = createNewDocument();
      }
      backupShadow.restore(docCache.getClientVersion(),
          docCache.getServerVersion(), docCache.getText());
    }
  }

  @Override
  protected void saveCache() {
    backupCache.putObject(new DocumentShadowCache(getId(),
        backupShadow.getText(), backupShadow.getVersion(),
        backupShadow.getTargetVersion()));
  }

  @Override
  protected DocumentShadow createNewDocument() {
    DocumentShadow cloneDocShadow;
    cloneDocShadow = new ServerDocumentShadowImpl(diffHandler,
        serverCacheFactory);
    return cloneDocShadow;
  }

}
